<template>
    <div class="app-container">
        <el-row style="height: 100%">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                <el-form-item label="套餐名称">
                    <el-input clearable v-model="queryParams.comboName" placeholder="请输入套餐名称" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="批次">
                    <el-input clearable v-model="queryParams.batch" style="width: 100px;" placeholder="输入批次" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="卡号">
                    <el-input clearable v-model="queryParams.cardNumber" placeholder="请输入卡号" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <!-- <el-form-item label="所在区域" prop="area">
                    <el-cascader style="width: 230px;" clearable placeholder="请选择" size="small" v-model="location"
                        :options="areaSelectData" :props="{
                            label: 'Name',
                            value: 'Name',
                            children: 'ChildList',
                            checkStrictly: false 
                        }"></el-cascader>
                </el-form-item> -->
                <!-- <el-form-item label="详细地址">
                    <el-input clearable v-model="queryParams.district" placeholder="请输入详细地址" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="收货人姓名">
                    <el-input clearable v-model="queryParams.receiverName" style="width: 120px;" placeholder="收货人姓名"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="收货人手机号">
                    <el-input clearable v-model="queryParams.receiverPhone" placeholder="收货人手机号"
                        @keyup.enter.native="handleQuery" />
                </el-form-item> -->
                <el-form-item label="状态">
                    <el-select clearable v-model="queryParams.status" placeholder="请选择">
                        <el-option v-for="item in optionsStatus" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="起止时间">
                    <el-date-picker v-model="dateRange" style="width: 350px;" size="small" @change="changeDate"
                        value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" range-separator="-"
                        start-placeholder="开始时间" end-placeholder="结束时间" clearable></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                    <el-button type="danger" plain icon="el-icon-plus" size="mini" @click="handleExport">导出</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="loading" :data="postList" height="85%">
                <el-table-column label="批次" align="center" prop="name">
                    <template slot-scope="scope">
                        <el-tag>{{ scope.row.batch }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="卡号" align="center" prop="cardNumber" />
                <el-table-column label="卡密" align="center" prop="cardKey" />
                <!-- <el-table-column label="comboId" align="center" prop="comboId" /> -->
                <el-table-column label="套餐名称" align="center" prop="comboName" />
                <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createdAt) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" width="180">
                    <template slot-scope="scope">
                        <span>{{ scope.row.status == 0 ? '未核销' : scope.row.status == 1 ? '已核销' : '已作废' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.status != 2" size="mini" type="text"
                            @click="handleDelete(scope.row)">作废卡片</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :pageSizes="[10, 20, 30, 50, 100]" :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-row>
        <!-- 添加或修改岗位对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="套餐" prop="comboId">
                    <el-select clearable v-model="form.comboId" placeholder="请选择套餐">
                        <el-option v-for="item in comboList" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="选择批次" prop="batch">
                    <el-input-number v-model="form.batch" :min="1" :max="9999" label="选择批次"></el-input-number>
                </el-form-item>
                <el-form-item label="数量" prop="number">
                    <el-input-number v-model="form.number" :min="1" :max="9999" label="数量"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { comboCardListApi, comboInsertCardApi, comboUpdateCardApi, comboListApi, cardExportDataApi } from "@/api/index"
export default {
    name: "Post",
    dicts: [],
    data() {
        return {
            fileType: ['png', 'jpg', 'jpeg'], // 文件类型
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 岗位表格数据
            postList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                batch: undefined,
                comboName: undefined,
                cardNumber: undefined,
                batch: undefined,
                status: undefined,
                startDate: "",
                endDate: ""
            },
            // 表单参数
            form: {
                comboId: "",
                batch: "",
                number: ""
            },
            // 表单校验
            rules: {
                comboId: [
                    { required: true, message: "套餐不能为空", trigger: "blur" }
                ],
                batch: [
                    { required: true, message: "批次不能为空", trigger: "blur" }
                ],
                number: [
                    { required: true, message: "数量不能为空", trigger: "blur" }
                ]

            },
            FileUploadVal: '',
            baseUrl: process.env.VUE_APP_BASE_API,
            dateRange: [],
            location: [],
            optionsStatus: [
                { value: 0, label: "未核销" },
                { value: 1, label: "已核销" },
                { value: 2, label: "作废" },
            ],
            comboList: [],//套餐列表
        }
    },
    created() {
        this.getList();
        this.comboListF();
    },
    methods: {
        changeDate() {
            console.log(this.dateRange, 'dateRange');
            if (this.dateRange) {

            } else {
                this.queryParams.startDate = "";
                this.queryParams.endDate = "";
            }
        },
        /** 查询产品列表 */
        comboListF() {
            this.loading = true
            comboListApi({}).then(response => {
                console.log(response, 'response');
                if (response.data) {
                    this.comboList = response.data.map(item => {
                        return {
                            value: item.id,
                            label: item.name
                        }
                    })
                    console.log(this.comboList, 'comboList');
                }
            })
        },
        FileUploadValChange(val) {
            console.log(val, 'FileUploadValChange');
            this.FileUploadVal = val;
            this.form.imageUrl = val[0].url; // 假设只上传一个文件
        },
        /** 查询产品列表 */
        getList() {
            this.loading = true
            comboCardListApi(this.queryParams).then(response => {
                console.log(response, 'response');
                this.postList = response.data.list;
                this.total = response.data.total;
                this.loading = false;
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                name: undefined,
                imageUrl: undefined,
                description: undefined,
            }
            this.resetForm("form")
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.province = this.location[0] ? this.location[0] : undefined;
            this.queryParams.city = this.location[1] ? this.location[1] : undefined;
            this.queryParams.district = this.location[2] ? this.location[2] : undefined;
            this.queryParams.pageNum = 1;
            if (this.dateRange && this.dateRange.length === 2) {
                this.queryParams.startDate = this.dateRange[0];
                this.queryParams.endDate = this.dateRange[1];
            }
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                comboName: undefined,
                cardNumber: undefined,
                batch: undefined,
                status: undefined,
                startDate: "",
                endDate: ""
            }
            this.dateRange = [];
            this.resetForm("queryForm")
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.postId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = "添加套餐";
            this.FileUploadVal = [];
        },
        /** 导出按钮操作 */
        async handleExport() {
            // window.open(process.env.VUE_APP_BASE_API + '/combo/card/export')
            this.download('combo/card/export', {
                ...this.queryParams
            }, `post_${new Date().getTime()}.xlsx`)
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            this.form = JSON.parse(JSON.stringify(row))
            this.open = true
            this.title = "修改商品";
            this.FileUploadVal = [
                {
                    url: row.imageUrl,
                    name: row.imageUrl.split('/').pop() // 获取文件名
                }
            ]
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    const params = this.form;
                    console.log(params, 'params');
                    comboInsertCardApi(params).then(response => {
                        this.$modal.msgSuccess("新增成功")
                        this.open = false
                        this.getList()
                    })
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.$modal.confirm('是否确认作废该卡片吗').then(function () {
                return comboUpdateCardApi({
                    id: row.id,
                    status: 2
                })
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess("作废成功")
            }).catch(() => { })
        },
    }
}
</script>
<style lang="scss" scoped>
.app-container {
    height: calc(100vh - 120px);
    // border: 1px solid #222;
}
</style>